package br.mg.bhe.vertice;

import java.util.Map;
import java.util.HashMap;
import java.io.DataInputStream;

public class Utils {

  private Utils() {
    super();
  }
  
  public static Map<String, String> getDownloadParameters(String query) {
    Map<String, String> map = new HashMap<String, String>();
    String[] params = query.split("&");
    
    for (String param : params) {
      String[] tokens = param.split("=");
      map.put(tokens[0], tokens[1]);
    }
    
    return map;
  }
  
  public static void exec(String command) {
    try {
      System.out.println("Executing command: " + command);
      
      Process process = Runtime.getRuntime().exec(command);
      DataInputStream dataInputStream = new DataInputStream(process.getInputStream());
      String line;

      while ((line = dataInputStream.readLine()) != null) {
        System.out.println(line);
      }
    } catch (Exception ex) {
      System.err.println(String.format("Could not execute command '%s'. Detail: %s", command, ex.getMessage()));
    }
  }
  
  public static void startOfficeServer() {
    System.out.println("Starting Office Server");
    exec("soffice -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\" -nofirststartwizard");
  }
  
  public static String getFileHome() {
    return System.getenv("HOME") + "/anexos";
  }
}
